package com.example.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.WaresTypeManage;
import com.example.demo.service.WaresTypeManageService;
import com.example.demo.utils.JsonResult;
import com.example.demo.utils.ResultUtil;
import com.example.demo.utils.TimeUtil;
import com.example.demo.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 货物类型管理
 */
@RestController
@RequestMapping(value = "/WaresTypeManage")
public class WaresTypeManageController {
    @Autowired
    private WaresTypeManageService waresTypeManageService;

    /**
     * 分页查询
     * @param current
     * @return
     */
    @RequestMapping(value = "/getWaresTypeManagePage",method = RequestMethod.GET)
    public JsonResult getWaresTypeManagePage(@RequestParam(value = "current") Integer current,
                                             @RequestParam(value = "size")Integer size){
        return ResultUtil.success("查询成功",waresTypeManageService.page(new Page<>(current,size)));
    }

    /**
     * 添加
     * @param waresTypeManage
     * @return
     */
    @RequestMapping(value = "/addWaresTypeManage",method = RequestMethod.POST)
    public JsonResult addWaresTypeManage(@RequestBody WaresTypeManage waresTypeManage){
        String userName = UserInfoUtil.gerUserName();
        String dateTime = TimeUtil.getDateTime();

        waresTypeManage.setByName(userName);
        waresTypeManage.setCreateDate(dateTime);
        waresTypeManage.setDel(0);

        boolean save = waresTypeManageService.save(waresTypeManage);
        if(save){
            return ResultUtil.success("添加成功");
        }else{
            return ResultUtil.fail("添加失败");
        }
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(value = "/delWaresTypeManage/{id}",method = RequestMethod.DELETE)
    public JsonResult delWaresTypeManage(@PathVariable Integer id){
        boolean b = waresTypeManageService.removeById(id);
        if(b){
            return ResultUtil.success("删除成功");
        }else{
            return ResultUtil.fail("删除失败");
        }
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/editWaresTypeManage",method = RequestMethod.PUT)
    public JsonResult editWaresTypeManage(@RequestBody WaresTypeManage waresTypeManage){
        String userName = UserInfoUtil.gerUserName();
        String dateTime = TimeUtil.getDateTime();

        waresTypeManage.setEditName(userName);
        waresTypeManage.setUpdateDate(dateTime);

        boolean b = waresTypeManageService.updateById(waresTypeManage);
        if(b){
            return ResultUtil.success("修改成功");
        }else{
            return ResultUtil.success("修改失败");
        }
    }

    /**
     *获取列表
     * @return
     */
    @RequestMapping(value = "/getWaresTypeManageList",method = RequestMethod.GET)
    public JsonResult getWaresTypeManageList(){
       return  ResultUtil.success("查询成功",waresTypeManageService.list(Wrappers.query(null)));
    }
}
